package com.justgame.project.generator.template.impl;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.config.FileOutConfig;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.justgame.project.generator.enums.TemplateType;
import com.justgame.project.generator.properties.GlobalProperties;
import com.justgame.project.generator.properties.TemplateProperties;
import com.justgame.project.generator.template.ITemplateResolve;
import com.justgame.project.generator.utils.NameUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import javax.annotation.Resource;

@Component
public class ControllerTemplateResolve implements ITemplateResolve {
    @Resource
    private TemplateProperties templateProperties;
    @Resource
    private GlobalProperties globalProperties;

    private String outputFilePath;

    @Override
    public TemplateType getType() {
        return TemplateType.CONTROLLER;
    }

    @Override
    public FileOutConfig buildConfig() {
        return new FileOutConfig(getTemplateFilePath()) {
            @Override
            public String outputFile(TableInfo tableInfo) {
                return getOutputFilePath(tableInfo.getName());
            }
        };
    }

    @Override
    public String getTemplateFilePath() {
        Assert.hasLength(templateProperties.getControllerTemplatePath(),"请先配置 Controller 模板文件路径");
        return templateProperties.getControllerTemplatePath();
    }

    @Override
    public String getOutputFilePath(String tableName) {
        String moduleName = NameUtils.getModuleNameByTableName(tableName);
        String entityName = NameUtils.getEntityNameByTableName(tableName);
        return globalProperties.getProjectPath() + globalProperties.getAfterProjectOperationPath() +
                "/" + NameUtils.formatModuleServiceName(moduleName) + "/src/main/java"+ globalProperties.getOperationPackage() + "/"+ moduleName +
                "/controller/" + entityName + "Controller" + StringPool.DOT_JAVA;
    }
}
